package com.whitedust.top30;

/**
 * @author A
 * @since 2025/11/3 19:15
 */

public class Top30 {
    public int trap(int[] height) {
        int[] pre = new int[height.length];
        int[] suf = new int[height.length];
        int preMax = 0;
        int sufMax = 0;
        for (int i = 0; i < height.length; i++) {
            preMax = Math.max(height[i], preMax);
            pre[i] = Math.max(height[i], preMax);
        }
        for (int i = height.length - 1; i >= 0; i--) {
            sufMax = Math.max(height[i], sufMax);
            suf[i] = Math.max(height[i], sufMax);
        }
        int res = 0;
        for (int i = 0; i < height.length; i++) {
            int h = Math.min(pre[i], suf[i]);
            res += h > height[i] ? h - height[i] : 0;
        }
        return res;
    }
}
